clear all;
close all;
Filenames={};
    
colors=lines(length(Filenames));


figure;
for fi=1:length(Filenames)
    filename=Filenames{fi};

    data=loadPalmSensXLSX(filename);
    [~,Index1khz]=min(abs(data(:,1)-1000));
    Impedance1kHz(fi)=data(Index1khz,2);
    % manually select the index at which the 1khz occurs
    subplot(2,1,1);
    hold on;
    plot(data(:,1),data(:,2),'color',colors(fi,:),'linewidth',2);
    hold on;
    
    subplot(2,1,2);
    hold on;
    plot(data(:,1),data(:,3),'color',colors(fi,:),'linewidth',2);
    hold on;
end

subplot(2,1,1);
set(gca,'FontName','Helvetica','Fontsize',20);
set(gca,'XScale','log','YScale','log');
xlabel('Frequency (Hz)');
ylabel('Z modulus (\Omega)');
xlim([0 10^6]);
ylim([10^2 10^7]);
yticks([10^2 10^3 10^4 10^5 10^6 10^7 10^8 10^9]);
grid on;
legend({ });

subplot(2,1,2);
set(gca,'FontName','Helvetica','Fontsize',20);
set(gca,'XScale','log');
xlabel('Frequency (Hz)');
ylabel('Z phase (\circ)');
grid on;
ylim([-90 20]);

figure;
set(gca,'FontName','Helvetica','fontsize',20);
set(gca,'XScale','log');
set(gca,'YScale','log');
plot(Impedance1kHz,'o','linewidth',2)
xlabel('electrode#');
ylabel('Norm (\Omega)');
title(' ');

%% Export to Excel

T{1,1}='File Name';
T{1,2}='Impedance at 1kHz (Ohm)';
for fi=1:length(Filenames)
    T{fi+1,1}=Filenames{fi};
    T{fi+1,2}=Impedance1kHz(fi);
end

filename = 'export_test.xlsx';
writecell(T,filename,'Sheet','Exported Data');




